package com.pwc.conversionTool.rest;

import static com.pwc.conversionTool.controller.sessionPersistence.AccountSessionPersistence.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.xml.soap.SOAPException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pwc.conversionTool.biz.SoapConnectionBiz;
import com.pwc.conversionTool.entity.Account;
import com.pwc.conversionTool.entity.DataTemplate;
import com.pwc.conversionTool.entity.Project;
import com.pwc.conversionTool.entity.SoapConnection;
import com.pwc.conversionTool.entity.SoapEntity;
import com.pwc.soapUtil.SoapUtil;

@Controller
public class SoapRest {

	private static final Logger logger = LoggerFactory.getLogger(SoapRest.class);

	private SoapUtil Util = new SoapUtil();

	private SoapConnectionBiz soapConnectionbiz = new SoapConnectionBiz();

	@Autowired
	public ServletContext context;

	@RequestMapping(value = "/rest/soap/sendSoap", method = RequestMethod.POST, produces = MediaType.ALL_VALUE)
	public @ResponseBody HashMap<String, String> SendSoap(@RequestBody SoapEntity soapEntity, HttpSession session) {

		HashMap<String, String> result = new HashMap<String, String>();

		try {
			result.put("result", Util.SendSoapBase(soapEntity.getPayload(), soapEntity.getURL(), soapEntity.getUserName(),
					soapEntity.getPassword(), soapEntity.getPasswordType()));

		} catch (UnsupportedOperationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SOAPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return result;

	}

	@RequestMapping(value = "/rest/soap/createConnection", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody boolean createSoapConnection(@RequestBody SoapConnection soapConnection, HttpSession session) {

		try {

			soapConnectionbiz.saveSoapConnection(soapConnection,getCurrentAccount(session) );

//			saveSoapConnection(session, soapConnection);

			return true;
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return false;
	}

	@RequestMapping(value = "/rest/soap/getAllconnection", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody List<SoapConnection> getAllconnection(HttpSession session) {

		
		
		List<SoapConnection> soapConnections=new ArrayList<SoapConnection>();
		try {
			
			soapConnections =soapConnectionbiz.getConnectionsByAccount(getCurrentAccount(session));
			
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
		}

		return soapConnections;
	}
	
	
	
	@RequestMapping(value = "/rest/soap/deleteSoapConnection", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody boolean deleteSoapConnection(@RequestBody SoapConnection soapConnection, HttpSession session) {
		
		try {
			
			soapConnectionbiz.deleteSoapConnection(soapConnection);
			
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
			return false;
		}
	}
	
	
	@RequestMapping(value = "/rest/soap/setcurrentSoapConnection", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public @ResponseBody boolean setCurrentSoapConnectionRest(@RequestBody SoapConnection soapConnection, HttpSession session) {
		
		try {
			
			SoapConnection currentConnection= soapConnectionbiz.getSoapConnectionById(soapConnection.getId());
			
//			saveSoapConnection(session,currentConnection);
			
			return true;
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error(Arrays.toString(e.getStackTrace()));
			return false;
		}
	} 
	
	
//	@RequestMapping(value = "/rest/soap/getcurrentSoapConnection", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
//	public @ResponseBody SoapConnection getCurrentSoapConnectionRest( HttpSession session) {
//		
//		try {
//			
//			return getCurrentSoapConnection(session);
//		} catch (Exception e) {
//			logger.error(e.getMessage());
//			logger.error(Arrays.toString(e.getStackTrace()));
//			return null;
//		}
//	}
	

}
